package com.siemens.industrial.web.mvc.dao.partner;

import com.siemens.industrial.model.partner.DeviceState;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Types;
import java.util.Date;
import java.util.List;

/**
 * Created by qupen on 2016/8/31.
 */
@Service
public class PartnerDeviceStateDao {
    @Resource
    JdbcTemplate jdbcTemplate;

    private static final String databaseTag = "{database}";
    private static final String saveDeviceStateQuery = "INSERT INTO `" + databaseTag + "`.`device_state` (`device_id`, `time`, `state`, `status_type`) VALUES (?,?,?,?)";
    private static final String getTimeCount ="SELECT count(*) FROM `" + databaseTag +"`.device_state where time=FROM_UNIXTIME(?/1000) and device_id=? and status_type=?;";
//    public DeviceState getDeviceState(String database, String deviceId, java.util.Date time) {
//        try {
//            return jdbcTemplate.queryForObject(getDeviceStateQuery.replace(databaseTag, database), new Object[]{deviceId, new java.sql.Date(time.getTime())}, new int[]{Types.VARCHAR, Types.DATE}, DeviceState.getRowMapper());
//        } catch (EmptyResultDataAccessException e) {
//            return null;
//        }
//    }
//
//    public DeviceState getDeviceStateByDevice(String database, String deviceId) {
//        try {
//            return jdbcTemplate.queryForObject(getDeviceStateByDeviceQuery.replace(databaseTag, database), new Object[]{deviceId}, new int[]{Types.VARCHAR}, DeviceState.getRowMapper());
//        } catch (EmptyResultDataAccessException e) {
//            return null;
//        }
//    }
//
//    public List<DeviceState> getDeviceStates(String database) {
//        return jdbcTemplate.query(getDeviceStatesQuery.replace(databaseTag, database), DeviceState.getRowMapper());
//    }
//
//    public List<DeviceState> getDeviceStatesInTime(String database, java.util.Date time) {
//        return jdbcTemplate.query(getDeviceStatesInTimeQuery.replace(databaseTag, database), new Object[]{new java.sql.Date(time.getTime())}, DeviceState.getRowMapper());
//    }
//
//    public List<DeviceState> getDeviceStatesInDevice(String database, String deviceId) {
//        return jdbcTemplate.query(getDeviceStatesInDeviceQuery.replace(databaseTag, database), new Object[]{deviceId}, DeviceState.getRowMapper());
//    }

    public void saveDeviceState(String database, DeviceState deviceState) {
        jdbcTemplate.update(saveDeviceStateQuery.replace(databaseTag, database), pss -> {
                    pss.setString(1, deviceState.getDeviceId());
                    pss.setTimestamp(2, new java.sql.Timestamp(deviceState.getTime().getTime()));
                    pss.setInt(3, deviceState.getState());
                    pss.setInt(4, deviceState.getStatusType());
                }
        );
    }

//    public void updateDeviceState(String database, DeviceState deviceState) {
//        jdbcTemplate.update(saveDeviceStateQuery.replace(databaseTag, database), pss -> {
//                    pss.setTimestamp(1, new java.sql.Timestamp(deviceState.getTime().getTime()));
//                    pss.setInt(2, deviceState.getState());
//                    pss.setString(3, deviceState.getDeviceId());
//                }
//        );
//    }

    public boolean isHaveTime(String database,String deviceId,long time,int statusType){
        int timeCount = jdbcTemplate.queryForObject(getTimeCount.replace(databaseTag,database),new Object[]{time,deviceId,statusType},Integer.class);
        return timeCount==1;
    }
}
